package com.ejuornal.action.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.ejuornal.util.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
 * 项目名称：libraryshare    
 * 类名称：UserLoginInterceptor    
 * 类描述：  用户登录是返回前一页面拦截器  
 * 创建人：陈晨    
 * 创建时间：Feb 22, 2013 1:18:24 PM    
 * 修改人：陈晨    
 * 修改时间：Feb 22, 2013 1:18:24 PM    
 * 修改备注：    
 * @version     
 */
public class UserLoginInterceptor_2 implements Interceptor{
    private static final long serialVersionUID = 4216557309026659292L;

    public void destroy() {
    }

    public void init() {
    }

    public String intercept(ActionInvocation invocation) throws Exception {
        ActionContext context = invocation.getInvocationContext();
        Map<String, Object> session = context.getSession();
        String user = (String) session.get(Constants.USERSESSION);
        //如果用户没有登录把当前页面的url放在sesssion当做登录以后返回的页面
        if(user == null) {
            HttpServletRequest request = ServletActionContext.getRequest();
            String path = request.getRequestURI();
            
            StringBuffer sb = new StringBuffer(path);
            String[] preActions = path.split("/");
            int length = preActions[2].length() + 11;
            
            String queryString = request.getQueryString();
            if(queryString == null) {
                queryString = "";
            }
            
            //通过字符串拼接得到登录之前的地址
            String realPath = path.substring(length) + "?" + queryString;
            //当前url的地址
            session.put("prePage", realPath);
            //当前url的action
            session.put("preAction", "/"+preActions[2]);
            return "error_userLogin";
        }
        return invocation.invoke();
    }
}
